llvm.org GIT mirror llvm / 6d8f2ca
Reapply r97010, the speculative revert failed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97036 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
4 changed file(s) with 37 addition(s) and 50 deletion(s). Raw diff Collapse all Expand all
6565 bool IsPotentiallyPHITranslatable() const;
6666
6767 /// PHITranslateValue - PHI translate the current address up the CFG from
68 /// CurBB to Pred, updating our state the reflect any needed changes. This
69 /// returns true on failure and sets Addr to null.
70 bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB);
68 /// CurBB to Pred, updating our state to reflect any needed changes. If the
69 /// dominator tree DT is non-null, the translated value must dominate
70 /// PredBB. This returns true on failure and sets Addr to null.
71 bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
72 const DominatorTree *DT);
7173
7274 /// PHITranslateWithInsertion - PHI translate this value into the specified
7375 /// predecessor block, inserting a computation of the value if it is
8789 /// returns false.
8890 bool Verify() const;
8991 private:
90 Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB);
91
92
93 /// GetAvailablePHITranslatedSubExpr - Return the value computed by
94 /// PHITranslateSubExpr if it dominates PredBB, otherwise return null.
95 Value *GetAvailablePHITranslatedSubExpr(Value *V,
96 BasicBlock *CurBB, BasicBlock *PredBB,
97 const DominatorTree &DT) const;
92 Value *PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB,
93 const DominatorTree *DT);
9894
9995 /// InsertPHITranslatedSubExpr - Insert a computation of the PHI translated
10096 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
860860 // Get the PHI translated pointer in this predecessor. This can fail if
861861 // not translatable, in which case the getAddr() returns null.
862862 PHITransAddr PredPointer(Pointer);
863 PredPointer.PHITranslateValue(BB, Pred);
863 PredPointer.PHITranslateValue(BB, Pred, 0);
864864
865865 Value *PredPtrVal = PredPointer.getAddr();
866866
133133 }
134134
135135 Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
136 BasicBlock *PredBB) {
136 BasicBlock *PredBB,
137 const DominatorTree *DT) {
137138 // If this is a non-instruction value, it can't require PHI translation.
138139 Instruction *Inst = dyn_cast(V);
139140 if (Inst == 0) return V;
176177 // operands need to be phi translated, and if so, reconstruct it.
177178
178179 if (BitCastInst *BC = dyn_cast(Inst)) {
179 Value *PHIIn = PHITranslateSubExpr(BC->getOperand(0), CurBB, PredBB);
180 Value *PHIIn = PHITranslateSubExpr(BC->getOperand(0), CurBB, PredBB, DT);
180181 if (PHIIn == 0) return 0;
181182 if (PHIIn == BC->getOperand(0))
182183 return BC;
192193 for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
193194 UI != E; ++UI) {
194195 if (BitCastInst *BCI = dyn_cast(*UI))
195 if (BCI->getType() == BC->getType())
196 if (BCI->getType() == BC->getType() &&
197 (!DT || DT->dominates(BCI->getParent(), PredBB)))
196198 return BCI;
197199 }
198200 return 0;
203205 SmallVector GEPOps;
204206 bool AnyChanged = false;
205207 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
206 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB);
208 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
207209 if (GEPOp == 0) return 0;
208210
209211 AnyChanged |= GEPOp != GEP->getOperand(i);
228230 if (GetElementPtrInst *GEPI = dyn_cast(*UI))
229231 if (GEPI->getType() == GEP->getType() &&
230232 GEPI->getNumOperands() == GEPOps.size() &&
231 GEPI->getParent()->getParent() == CurBB->getParent()) {
233 GEPI->getParent()->getParent() == CurBB->getParent() &&
234 (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
232235 bool Mismatch = false;
233236 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
234237 if (GEPI->getOperand(i) != GEPOps[i]) {
250253 bool isNSW = cast(Inst)->hasNoSignedWrap();
251254 bool isNUW = cast(Inst)->hasNoUnsignedWrap();
252255
253 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB);
256 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
254257 if (LHS == 0) return 0;
255258
256259 // If the PHI translated LHS is an add of a constant, fold the immediates.
286289 if (BinaryOperator *BO = dyn_cast(*UI))
287290 if (BO->getOpcode() == Instruction::Add &&
288291 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
289 BO->getParent()->getParent() == CurBB->getParent())
292 BO->getParent()->getParent() == CurBB->getParent() &&
293 (!DT || DT->dominates(BO->getParent(), PredBB)))
290294 return BO;
291295 }
292296
299303
300304
301305 /// PHITranslateValue - PHI translate the current address up the CFG from
302 /// CurBB to Pred, updating our state the reflect any needed changes. This
303 /// returns true on failure and sets Addr to null.
304 bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB) {
306 /// CurBB to Pred, updating our state to reflect any needed changes. If the
307 /// dominator tree DT is non-null, the translated value must dominate
308 /// PredBB. This returns true on failure and sets Addr to null.
309 bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
310 const DominatorTree *DT) {
305311 assert(Verify() && "Invalid PHITransAddr!");
306 Addr = PHITranslateSubExpr(Addr, CurBB, PredBB);
312 Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT);
307313 assert(Verify() && "Invalid PHITransAddr!");
314
315 if (DT) {
316 // Make sure the value is live in the predecessor.
317 if (Instruction *Inst = dyn_cast_or_null(Addr))
318 if (!DT->dominates(Inst->getParent(), PredBB))
319 Addr = 0;
320 }
321
308322 return Addr == 0;
309323 }
310
311 /// GetAvailablePHITranslatedSubExpr - Return the value computed by
312 /// PHITranslateSubExpr if it dominates PredBB, otherwise return null.
313 Value *PHITransAddr::
314 GetAvailablePHITranslatedSubExpr(Value *V, BasicBlock *CurBB,BasicBlock *PredBB,
315 const DominatorTree &DT) const {
316 PHITransAddr Tmp(V, TD);
317 Tmp.PHITranslateValue(CurBB, PredBB);
318
319 // See if PHI translation succeeds.
320 V = Tmp.getAddr();
321
322 // Make sure the value is live in the predecessor.
323 if (Instruction *Inst = dyn_cast_or_null(V))
324 if (!DT.dominates(Inst->getParent(), PredBB))
325 return 0;
326 return V;
327 }
328
329324
330325 /// PHITranslateWithInsertion - PHI translate this value into the specified
331326 /// predecessor block, inserting a computation of the value if it is
364359 SmallVectorImpl &NewInsts) {
365360 // See if we have a version of this value already available and dominating
366361 // PredBB. If so, there is no need to insert a new instance of it.
367 if (Value *Res = GetAvailablePHITranslatedSubExpr(InVal, CurBB, PredBB, DT))
368 return Res;
362 PHITransAddr Tmp(InVal, TD);
363 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT))
364 return Tmp.getAddr();
369365
370366 // If we don't have an available version of this value, it must be an
371367 // instruction.
16321632 LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred,
16331633 *DT, NewInsts);
16341634 } else {
1635 Address.PHITranslateValue(LoadBB, UnavailablePred);
1635 Address.PHITranslateValue(LoadBB, UnavailablePred, DT);
16361636 LoadPtr = Address.getAddr();
1637
1638 // Make sure the value is live in the predecessor.
1639 if (Instruction *Inst = dyn_cast_or_null(LoadPtr))
1640 if (!DT->dominates(Inst->getParent(), UnavailablePred))
1641 LoadPtr = 0;
16421637 }
16431638
16441639 // If we couldn't find or insert a computation of this phi translated value,