@@ -42,242 +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 (), EI = BasicBlock::iterator (BB->getFirstNonPHI ());
184- II != EI; II++, NumPhis++) {
185- // FIXME: consider handling this in AggressiveInstCombine
186- if (NumPhis > MaxNumPhis)
187- return nullptr ;
188- PHINode *PtrPHI = dyn_cast<PHINode>(II);
189- if (!PtrPHI || PtrPHI == &PN || PtrPHI->getType () != IntToPtr->getType ())
190- continue ;
191- MatchingPtrPHI = PtrPHI;
192- for (unsigned i = 0 ; i != PtrPHI->getNumIncomingValues (); ++i) {
193- if (AvailablePtrVals[i] !=
194- PtrPHI->getIncomingValueForBlock (PN.getIncomingBlock (i))) {
195- MatchingPtrPHI = nullptr ;
196- break ;
197- }
198- }
199-
200- if (MatchingPtrPHI)
201- break ;
202- }
203-
204- if (MatchingPtrPHI) {
205- assert (MatchingPtrPHI->getType () == IntToPtr->getType () &&
206- " Phi's Type does not match with IntToPtr" );
207- // The PtrToCast + IntToPtr will be simplified later
208- return CastInst::CreateBitOrPointerCast (MatchingPtrPHI,
209- IntToPtr->getOperand (0 )->getType ());
210- }
211-
212- // If it requires a conversion for every PHI operand, do not do it.
213- if (all_of (AvailablePtrVals, [&](Value *V) {
214- return (V->getType () != IntToPtr->getType ()) || isa<IntToPtrInst>(V);
215- }))
216- return nullptr ;
217-
218- // If any of the operand that requires casting is a terminator
219- // instruction, do not do it.
220- if (any_of (AvailablePtrVals, [&](Value *V) {
221- if (V->getType () == IntToPtr->getType ())
222- return false ;
223-
224- auto *Inst = dyn_cast<Instruction>(V);
225- return Inst && Inst->isTerminator ();
226- }))
227- return nullptr ;
228-
229- PHINode *NewPtrPHI = PHINode::Create (
230- IntToPtr->getType (), PN.getNumIncomingValues (), PN.getName () + " .ptr" );
231-
232- InsertNewInstBefore (NewPtrPHI, PN);
233- SmallDenseMap<Value *, Instruction *> Casts;
234- for (unsigned i = 0 ; i != PN.getNumIncomingValues (); ++i) {
235- auto *IncomingBB = PN.getIncomingBlock (i);
236- auto *IncomingVal = AvailablePtrVals[i];
237-
238- if (IncomingVal->getType () == IntToPtr->getType ()) {
239- NewPtrPHI->addIncoming (IncomingVal, IncomingBB);
240- continue ;
241- }
242-
243- #ifndef NDEBUG
244- LoadInst *LoadI = dyn_cast<LoadInst>(IncomingVal);
245- assert ((isa<PHINode>(IncomingVal) ||
246- IncomingVal->getType ()->isPointerTy () ||
247- (LoadI && LoadI->hasOneUse ())) &&
248- " Can not replace LoadInst with multiple uses" );
249- #endif
250- // Need to insert a BitCast.
251- // For an integer Load instruction with a single use, the load + IntToPtr
252- // cast will be simplified into a pointer load:
253- // %v = load i64, i64* %a.ip, align 8
254- // %v.cast = inttoptr i64 %v to float **
255- // ==>
256- // %v.ptrp = bitcast i64 * %a.ip to float **
257- // %v.cast = load float *, float ** %v.ptrp, align 8
258- Instruction *&CI = Casts[IncomingVal];
259- if (!CI) {
260- CI = CastInst::CreateBitOrPointerCast (IncomingVal, IntToPtr->getType (),
261- IncomingVal->getName () + " .ptr" );
262- if (auto *IncomingI = dyn_cast<Instruction>(IncomingVal)) {
263- BasicBlock::iterator InsertPos (IncomingI);
264- InsertPos++;
265- if (isa<PHINode>(IncomingI))
266- InsertPos = IncomingI->getParent ()->getFirstInsertionPt ();
267- InsertNewInstBefore (CI, *InsertPos);
268- } else {
269- auto *InsertBB = &IncomingBB->getParent ()->getEntryBlock ();
270- InsertNewInstBefore (CI, *InsertBB->getFirstInsertionPt ());
271- }
272- }
273- NewPtrPHI->addIncoming (CI, IncomingBB);
274- }
275-
276- // The PtrToCast + IntToPtr will be simplified later
277- return CastInst::CreateBitOrPointerCast (NewPtrPHI,
278- IntToPtr->getOperand (0 )->getType ());
279- }
280-
28145// / If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the
28246// / adds all have a single use, turn this into a phi and a single binop.
28347Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI (PHINode &PN) {
@@ -1142,9 +906,6 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) {
1142906 // this PHI only has a single use (a PHI), and if that PHI only has one use (a
1143907 // PHI)... break the cycle.
1144908 if (PN.hasOneUse ()) {
1145- if (Instruction *Result = FoldIntegerTypedPHI (PN))
1146- return Result;
1147-
1148909 Instruction *PHIUser = cast<Instruction>(PN.user_back ());
1149910 if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
1150911 SmallPtrSet<PHINode*, 16 > PotentiallyDeadPHIs;
0 commit comments