@@ -42,243 +42,6 @@ void InstCombiner::PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN) {
4242 }
4343}
4444
45- // Replace Integer typed PHI PN if the PHI's value is used as a pointer value.
46- // If there is an existing pointer typed PHI that produces the same value as PN,
47- // replace PN and the IntToPtr operation with it. Otherwise, synthesize a new
48- // PHI node:
49- //
50- // Case-1:
51- // bb1:
52- // int_init = PtrToInt(ptr_init)
53- // br label %bb2
54- // bb2:
55- // int_val = PHI([int_init, %bb1], [int_val_inc, %bb2]
56- // ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]
57- // ptr_val2 = IntToPtr(int_val)
58- // ...
59- // use(ptr_val2)
60- // ptr_val_inc = ...
61- // inc_val_inc = PtrToInt(ptr_val_inc)
62- //
63- // ==>
64- // bb1:
65- // br label %bb2
66- // bb2:
67- // ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]
68- // ...
69- // use(ptr_val)
70- // ptr_val_inc = ...
71- //
72- // Case-2:
73- // bb1:
74- // int_ptr = BitCast(ptr_ptr)
75- // int_init = Load(int_ptr)
76- // br label %bb2
77- // bb2:
78- // int_val = PHI([int_init, %bb1], [int_val_inc, %bb2]
79- // ptr_val2 = IntToPtr(int_val)
80- // ...
81- // use(ptr_val2)
82- // ptr_val_inc = ...
83- // inc_val_inc = PtrToInt(ptr_val_inc)
84- // ==>
85- // bb1:
86- // ptr_init = Load(ptr_ptr)
87- // br label %bb2
88- // bb2:
89- // ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]
90- // ...
91- // use(ptr_val)
92- // ptr_val_inc = ...
93- // ...
94- //
95- Instruction *InstCombiner::FoldIntegerTypedPHI (PHINode &PN) {
96- if (!PN.getType ()->isIntegerTy ())
97- return nullptr ;
98- if (!PN.hasOneUse ())
99- return nullptr ;
100-
101- auto *IntToPtr = dyn_cast<IntToPtrInst>(PN.user_back ());
102- if (!IntToPtr)
103- return nullptr ;
104-
105- // Check if the pointer is actually used as pointer:
106- auto HasPointerUse = [](Instruction *IIP) {
107- for (User *U : IIP->users ()) {
108- Value *Ptr = nullptr ;
109- if (LoadInst *LoadI = dyn_cast<LoadInst>(U)) {
110- Ptr = LoadI->getPointerOperand ();
111- } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
112- Ptr = SI->getPointerOperand ();
113- } else if (GetElementPtrInst *GI = dyn_cast<GetElementPtrInst>(U)) {
114- Ptr = GI->getPointerOperand ();
115- }
116-
117- if (Ptr && Ptr == IIP)
118- return true ;
119- }
120- return false ;
121- };
122-
123- if (!HasPointerUse (IntToPtr))
124- return nullptr ;
125-
126- if (DL.getPointerSizeInBits (IntToPtr->getAddressSpace ()) !=
127- DL.getTypeSizeInBits (IntToPtr->getOperand (0 )->getType ()))
128- return nullptr ;
129-
130- SmallVector<Value *, 4 > AvailablePtrVals;
131- for (unsigned i = 0 ; i != PN.getNumIncomingValues (); ++i) {
132- Value *Arg = PN.getIncomingValue (i);
133-
134- // First look backward:
135- if (auto *PI = dyn_cast<PtrToIntInst>(Arg)) {
136- AvailablePtrVals.emplace_back (PI->getOperand (0 ));
137- continue ;
138- }
139-
140- // Next look forward:
141- Value *ArgIntToPtr = nullptr ;
142- for (User *U : Arg->users ()) {
143- if (isa<IntToPtrInst>(U) && U->getType () == IntToPtr->getType () &&
144- (DT.dominates (cast<Instruction>(U), PN.getIncomingBlock (i)) ||
145- cast<Instruction>(U)->getParent () == PN.getIncomingBlock (i))) {
146- ArgIntToPtr = U;
147- break ;
148- }
149- }
150-
151- if (ArgIntToPtr) {
152- AvailablePtrVals.emplace_back (ArgIntToPtr);
153- continue ;
154- }
155-
156- // If Arg is defined by a PHI, allow it. This will also create
157- // more opportunities iteratively.
158- if (isa<PHINode>(Arg)) {
159- AvailablePtrVals.emplace_back (Arg);
160- continue ;
161- }
162-
163- // For a single use integer load:
164- auto *LoadI = dyn_cast<LoadInst>(Arg);
165- if (!LoadI)
166- return nullptr ;
167-
168- if (!LoadI->hasOneUse ())
169- return nullptr ;
170-
171- // Push the integer typed Load instruction into the available
172- // value set, and fix it up later when the pointer typed PHI
173- // is synthesized.
174- AvailablePtrVals.emplace_back (LoadI);
175- }
176-
177- // Now search for a matching PHI
178- auto *BB = PN.getParent ();
179- assert (AvailablePtrVals.size () == PN.getNumIncomingValues () &&
180- " Not enough available ptr typed incoming values" );
181- PHINode *MatchingPtrPHI = nullptr ;
182- unsigned NumPhis = 0 ;
183- for (auto II = BB->begin (); II != BB->end (); II++, NumPhis++) {
184- // FIXME: consider handling this in AggressiveInstCombine
185- PHINode *PtrPHI = dyn_cast<PHINode>(II);
186- if (!PtrPHI)
187- break ;
188- if (NumPhis > MaxNumPhis)
189- return nullptr ;
190- if (PtrPHI == &PN || PtrPHI->getType () != IntToPtr->getType ())
191- continue ;
192- MatchingPtrPHI = PtrPHI;
193- for (unsigned i = 0 ; i != PtrPHI->getNumIncomingValues (); ++i) {
194- if (AvailablePtrVals[i] !=
195- PtrPHI->getIncomingValueForBlock (PN.getIncomingBlock (i))) {
196- MatchingPtrPHI = nullptr ;
197- break ;
198- }
199- }
200-
201- if (MatchingPtrPHI)
202- break ;
203- }
204-
205- if (MatchingPtrPHI) {
206- assert (MatchingPtrPHI->getType () == IntToPtr->getType () &&
207- " Phi's Type does not match with IntToPtr" );
208- // The PtrToCast + IntToPtr will be simplified later
209- return CastInst::CreateBitOrPointerCast (MatchingPtrPHI,
210- IntToPtr->getOperand (0 )->getType ());
211- }
212-
213- // If it requires a conversion for every PHI operand, do not do it.
214- if (all_of (AvailablePtrVals, [&](Value *V) {
215- return (V->getType () != IntToPtr->getType ()) || isa<IntToPtrInst>(V);
216- }))
217- return nullptr ;
218-
219- // If any of the operand that requires casting is a terminator
220- // instruction, do not do it.
221- if (any_of (AvailablePtrVals, [&](Value *V) {
222- if (V->getType () == IntToPtr->getType ())
223- return false ;
224-
225- auto *Inst = dyn_cast<Instruction>(V);
226- return Inst && Inst->isTerminator ();
227- }))
228- return nullptr ;
229-
230- PHINode *NewPtrPHI = PHINode::Create (
231- IntToPtr->getType (), PN.getNumIncomingValues (), PN.getName () + " .ptr" );
232-
233- InsertNewInstBefore (NewPtrPHI, PN);
234- SmallDenseMap<Value *, Instruction *> Casts;
235- for (unsigned i = 0 ; i != PN.getNumIncomingValues (); ++i) {
236- auto *IncomingBB = PN.getIncomingBlock (i);
237- auto *IncomingVal = AvailablePtrVals[i];
238-
239- if (IncomingVal->getType () == IntToPtr->getType ()) {
240- NewPtrPHI->addIncoming (IncomingVal, IncomingBB);
241- continue ;
242- }
243-
244- #ifndef NDEBUG
245- LoadInst *LoadI = dyn_cast<LoadInst>(IncomingVal);
246- assert ((isa<PHINode>(IncomingVal) ||
247- IncomingVal->getType ()->isPointerTy () ||
248- (LoadI && LoadI->hasOneUse ())) &&
249- " Can not replace LoadInst with multiple uses" );
250- #endif
251- // Need to insert a BitCast.
252- // For an integer Load instruction with a single use, the load + IntToPtr
253- // cast will be simplified into a pointer load:
254- // %v = load i64, i64* %a.ip, align 8
255- // %v.cast = inttoptr i64 %v to float **
256- // ==>
257- // %v.ptrp = bitcast i64 * %a.ip to float **
258- // %v.cast = load float *, float ** %v.ptrp, align 8
259- Instruction *&CI = Casts[IncomingVal];
260- if (!CI) {
261- CI = CastInst::CreateBitOrPointerCast (IncomingVal, IntToPtr->getType (),
262- IncomingVal->getName () + " .ptr" );
263- if (auto *IncomingI = dyn_cast<Instruction>(IncomingVal)) {
264- BasicBlock::iterator InsertPos (IncomingI);
265- InsertPos++;
266- if (isa<PHINode>(IncomingI))
267- InsertPos = IncomingI->getParent ()->getFirstInsertionPt ();
268- InsertNewInstBefore (CI, *InsertPos);
269- } else {
270- auto *InsertBB = &IncomingBB->getParent ()->getEntryBlock ();
271- InsertNewInstBefore (CI, *InsertBB->getFirstInsertionPt ());
272- }
273- }
274- NewPtrPHI->addIncoming (CI, IncomingBB);
275- }
276-
277- // The PtrToCast + IntToPtr will be simplified later
278- return CastInst::CreateBitOrPointerCast (NewPtrPHI,
279- IntToPtr->getOperand (0 )->getType ());
280- }
281-
28245// / If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the
28346// / adds all have a single use, turn this into a phi and a single binop.
28447Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI (PHINode &PN) {
@@ -1143,9 +906,6 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) {
1143906 // this PHI only has a single use (a PHI), and if that PHI only has one use (a
1144907 // PHI)... break the cycle.
1145908 if (PN.hasOneUse ()) {
1146- if (Instruction *Result = FoldIntegerTypedPHI (PN))
1147- return Result;
1148-
1149909 Instruction *PHIUser = cast<Instruction>(PN.user_back ());
1150910 if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
1151911 SmallPtrSet<PHINode*, 16 > PotentiallyDeadPHIs;
0 commit comments