LLVM: lib/Transforms/Utils/SSAUpdater.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

32#include

33#include

34

35using namespace llvm;

36

37#define DEBUG_TYPE "ssaupdater"

38

40

44

46 : InsertedPHIs(NewPHI) {}

47

51

53 if (!AV)

55 else

57 ProtoType = Ty;

58 ProtoName = std::string(Name);

59}

60

64

68

70 assert(ProtoType && "Need to initialize SSAUpdater");

71 assert(ProtoType == V->getType() &&

72 "All rewritten values must have the same type");

74}

75

78 unsigned PHINumValues = PHI->getNumIncomingValues();

79 if (PHINumValues != ValueMapping.size())

80 return false;

81

82

83 for (unsigned i = 0, e = PHINumValues; i != e; ++i)

84 if (ValueMapping[PHI->getIncomingBlock(i)] !=

85 PHI->getIncomingValue(i)) {

86 return false;

87 }

88

89 return true;

90}

91

93 Value *Res = GetValueAtEndOfBlockInternal(BB);

94 return Res;

95}

96

98

99

102

103

104

106 Value *SingularValue = nullptr;

107

108

109

110

112 for (unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) {

113 BasicBlock *PredBB = SomePhi->getIncomingBlock(i);

115 PredValues.push_back(std::make_pair(PredBB, PredVal));

116

117

118 if (i == 0)

119 SingularValue = PredVal;

120 else if (PredVal != SingularValue)

121 SingularValue = nullptr;

122 }

123 } else {

124 bool isFirstPred = true;

127 PredValues.push_back(std::make_pair(PredBB, PredVal));

128

129

130 if (isFirstPred) {

131 SingularValue = PredVal;

132 isFirstPred = false;

133 } else if (PredVal != SingularValue)

134 SingularValue = nullptr;

135 }

136 }

137

138

139 if (PredValues.empty())

141

142

143 if (SingularValue)

144 return SingularValue;

145

146

147

150 PredValues.end());

153 return &SomePHI;

154 }

155 }

156

157

161

162

163 for (const auto &PredValue : PredValues)

164 InsertedPHI->addIncoming(PredValue.second, PredValue.first);

165

166

167

171 return V;

172 }

173

174

177 DL = It->getDebugLoc();

179

180

181 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);

182

183 LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");

184 return InsertedPHI;

185}

186

189

193 else

195

196 U.set(V);

197}

198

202 for (auto &DVR : DbgVariableRecords) {

203 if (DVR->getParent() == I->getParent())

204 continue;

205 UpdateDebugValue(I, DVR);

206 }

207}

208

211 for (auto &DVR : DbgVariableRecords) {

212 UpdateDebugValue(I, DVR);

213 }

214}

215

221 } else

223}

224

227

231 else

233

234 U.set(V);

235}

236

237namespace llvm {

238

239template<>

241public:

246

249

251 private:

253 unsigned idx;

254

255 public:

259 : PHI(P), idx(PHI->getNumIncomingValues()) {}

260

264

267 };

268

273

274

275

278

279

280

283 else

285 }

286

287

288

292

293

294

298 PHINode::Create(Updater->ProtoType, NumPreds, Updater->ProtoName);

299

300

301

302

304 PHI->insertBefore(BB->begin());

305 return PHI;

306 }

307

308

309

311 PHI->addIncoming(Val, Pred);

312 }

313

314

318

319

320

323 if (PHI && PHI->getNumIncomingValues() == 0)

324 return PHI;

325 return nullptr;

326 }

327

328

329

333};

334

335}

336

337

338

339

340Value *SSAUpdater::GetValueAtEndOfBlockInternal(BasicBlock *BB) {

342 if (Value *V = AvailableVals[BB])

343 return V;

344

345 SSAUpdaterImpl Impl(this, &AvailableVals, InsertedPHIs);

346 return Impl.GetValue(BB);

347}

348

349

350

351

352

356 if (Insts.empty()) return;

357

358 const Value *SomeVal;

360 SomeVal = LI;

361 else

363

364 if (BaseName.empty())

365 BaseName = SomeVal->getName();

366 SSA.Initialize(SomeVal->getType(), BaseName);

367}

368

370

371

372

374

376 UsesByBlock[User->getParent()].push_back(User);

377

378

379

380

383

387

388

389 if (BlockUses.empty()) continue;

390

391

392

393 if (BlockUses.size() == 1) {

394

397 SSA.AddAvailableValue(BB, SI->getOperand(0));

399

401 } else {

402

404 }

405 BlockUses.clear();

406 continue;

407 }

408

409

410 bool HasStore = false;

413 HasStore = true;

414 break;

415 }

416 }

417

418

419 if (!HasStore) {

422 BlockUses.clear();

423 continue;

424 }

425

426

427

429 BlockUses.begin(), BlockUses.end(),

431

432

433

434

435

436

437 Value *StoredValue = nullptr;

440

441

442 if (StoredValue) {

444 L->replaceAllUsesWith(StoredValue);

445 ReplacedLoads[L] = StoredValue;

446 } else {

448 }

449 continue;

450 }

451

454

455

456 StoredValue = SI->getOperand(0);

458

459

461 }

462 }

463

464

465 assert(StoredValue && "Already checked that there is a store in block");

466 SSA.AddAvailableValue(BB, StoredValue);

467 BlockUses.clear();

468 }

469

470

471

472 for (LoadInst *ALoad : LiveInLoads) {

473 Value *NewVal = SSA.GetValueInMiddleOfBlock(ALoad->getParent());

475

476

478 ALoad->replaceAllUsesWith(NewVal);

479 ReplacedLoads[ALoad] = NewVal;

480 }

481

482

484

485

486

489 continue;

490

491

492

493

494

496 Value *NewVal = ReplacedLoads[User];

497 assert(NewVal && "not a replaced load?");

498

499

500

501

503 while (RLI != ReplacedLoads.end()) {

504 NewVal = RLI->second;

505 RLI = ReplacedLoads.find(NewVal);

506 }

507

510 }

511

513 User->eraseFromParent();

514 }

515}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL

static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

This file contains the declarations for the subclasses of Constant, which represent the different fla...

This file defines the DenseMap class.

This file provides various utilities for inspecting and working with the control flow graph in LLVM I...

This defines the Use class.

static AvailableValsTy & getAvailableVals(void *AV)

DenseMap< MachineBasicBlock *, Register > AvailableValsTy

static AvailableValsTy & getAvailableVals(void *AV)

Definition SSAUpdater.cpp:41

static bool IsEquivalentPHI(PHINode *PHI, SmallDenseMap< BasicBlock *, Value *, 8 > &ValueMapping)

Definition SSAUpdater.cpp:76

This file defines the SmallVector class.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

bool empty() const

empty - Check if the array is empty.

LLVM Basic Block Representation.

iterator begin()

Instruction iterator methods.

iterator_range< const_phi_iterator > phis() const

Returns a range that iterates over the phis in the basic block.

LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const

Returns an iterator to the first instruction in this block that is not a PHINode instruction.

LLVM_ABI const DataLayout & getDataLayout() const

Get the data layout of the module this basic block belongs to.

InstListType::iterator iterator

Instruction iterators...

LLVM_ABI const BasicBlock * getParent() const

Record of a variable value-assignment, aka a non instruction representation of the dbg....

LLVM_ABI void replaceVariableLocationOp(Value *OldValue, Value *NewValue, bool AllowEmpty=false)

LLVM_ABI void setKillLocation()

static DebugLoc getUnknown()

ValueT lookup(const_arg_type_t< KeyT > Val) const

lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...

iterator find(const_arg_type_t< KeyT > Val)

DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator

size_type count(const_arg_type_t< KeyT > Val) const

Return 1 if the specified key is in the map, 0 otherwise.

LLVM_ABI void insertBefore(InstListType::iterator InsertPos)

Insert an unlinked instruction into a basic block immediately before the specified position.

LLVM_ABI InstListType::iterator eraseFromParent()

This method unlinks 'this' from the containing basic block and deletes it.

void setDebugLoc(DebugLoc Loc)

Set the debug location information for this instruction.

An instruction for reading from memory.

void addIncoming(Value *V, BasicBlock *BB)

Add an incoming value to the end of the PHI list.

static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)

Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...

static LLVM_ABI PoisonValue * get(Type *T)

Static factory methods - Return an 'poison' object of the specified type.

Definition SSAUpdater.cpp:250

Value * getIncomingValue()

Definition SSAUpdater.cpp:265

BasicBlock * getIncomingBlock()

Definition SSAUpdater.cpp:266

PHI_iterator(PHINode *P)

Definition SSAUpdater.cpp:256

PHI_iterator(PHINode *P, bool)

Definition SSAUpdater.cpp:258

PHI_iterator & operator++()

Definition SSAUpdater.cpp:261

bool operator!=(const PHI_iterator &x) const

Definition SSAUpdater.cpp:263

bool operator==(const PHI_iterator &x) const

Definition SSAUpdater.cpp:262

static BlkSucc_iterator BlkSucc_end(BlkT *BB)

Definition SSAUpdater.cpp:248

static BlkSucc_iterator BlkSucc_begin(BlkT *BB)

Definition SSAUpdater.cpp:247

static void FindPredecessorBlocks(BasicBlock *BB, SmallVectorImpl< BasicBlock * > *Preds)

FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds vector, set Info->NumPreds,...

Definition SSAUpdater.cpp:276

static Value * CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds, SSAUpdater *Updater)

CreateEmptyPHI - Create a new PHI instruction in the specified block.

Definition SSAUpdater.cpp:295

static PHINode * ValueIsNewPHI(Value *Val, SSAUpdater *Updater)

ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source operands, i....

Definition SSAUpdater.cpp:321

static PHI_iterator PHI_begin(PhiT *PHI)

Definition SSAUpdater.cpp:269

succ_iterator BlkSucc_iterator

Definition SSAUpdater.cpp:245

BasicBlock BlkT

Definition SSAUpdater.cpp:242

static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred)

AddPHIOperand - Add the specified value as an operand of the PHI for the specified predecessor block.

Definition SSAUpdater.cpp:310

static Value * GetPHIValue(PHINode *PHI)

GetPHIValue - For the specified PHI instruction, return the value that it defines.

Definition SSAUpdater.cpp:330

static Value * GetPoisonVal(BasicBlock *BB, SSAUpdater *Updater)

GetPoisonVal - Get a poison value of the same type as the value being handled.

Definition SSAUpdater.cpp:289

PHINode PhiT

Definition SSAUpdater.cpp:244

static PHI_iterator PHI_end(PhiT *PHI)

Definition SSAUpdater.cpp:270

static PHINode * ValueIsPHI(Value *Val, SSAUpdater *Updater)

ValueIsPHI - Check if a value is a PHI.

Definition SSAUpdater.cpp:315

Value * ValT

Definition SSAUpdater.cpp:243

Helper class for SSA formation on a set of values defined in multiple blocks.

void RewriteUse(Use &U)

Rewrite a use of the symbolic value.

Definition SSAUpdater.cpp:187

void RewriteUseAfterInsertions(Use &U)

Rewrite a use like RewriteUse but handling in-block definitions.

Definition SSAUpdater.cpp:225

Value * FindValueForBlock(BasicBlock *BB) const

Return the value for the specified block if the SSAUpdater has one, otherwise return nullptr.

Definition SSAUpdater.cpp:65

void Initialize(Type *Ty, StringRef Name)

Reset this object to get ready for a new set of SSA updates with type 'Ty'.

Definition SSAUpdater.cpp:52

~SSAUpdater()

Definition SSAUpdater.cpp:48

Value * GetValueInMiddleOfBlock(BasicBlock *BB)

Construct SSA form, materializing a value that is live in the middle of the specified block.

Definition SSAUpdater.cpp:97

SSAUpdater(SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)

If InsertedPHIs is specified, it will be filled in with all PHI Nodes created by rewriting.

Definition SSAUpdater.cpp:45

void UpdateDebugValues(Instruction *I)

Rewrite debug value intrinsics to conform to a new SSA form.

Definition SSAUpdater.cpp:199

bool HasValueForBlock(BasicBlock *BB) const

Return true if the SSAUpdater already has a value for the specified block.

Definition SSAUpdater.cpp:61

Value * GetValueAtEndOfBlock(BasicBlock *BB)

Construct SSA form, materializing a value that is live at the end of the specified block.

Definition SSAUpdater.cpp:92

void AddAvailableValue(BasicBlock *BB, Value *V)

Indicate that a rewritten value is available in the specified block with the specified value.

Definition SSAUpdater.cpp:69

This class consists of common code factored out of the SmallVector class to reduce code duplication b...

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

An instruction for storing to memory.

StringRef - Represent a constant reference to a string, i.e.

constexpr bool empty() const

empty - Check if the string is empty.

TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...

The instances of the Type class are immutable: once they are created, they are never changed.

A Use represents the edge between a Value definition and its users.

LLVM Value Representation.

Type * getType() const

All values are typed, get the type of this value.

LLVM_ABI void replaceAllUsesWith(Value *V)

Change all uses of this to point to a new Value.

This is an optimization pass for GlobalISel generic memory operations.

FunctionAddr VTableAddr Value

LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)

Finds the dbg.values describing a value.

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

void append_range(Container &C, Range &&R)

Wrapper function to append range R to container C.

bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)

LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)

See if we can compute a simplified version of this instruction.

void sort(IteratorTy Start, IteratorTy End)

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

bool isa(const From &Val)

isa - Return true if the parameter to the template is an instance of one of the template type argu...

RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)

RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

auto predecessors(const MachineBasicBlock *BB)

SuccIterator< Instruction, BasicBlock > succ_iterator