Skip to content

Commit 90e3ac6

Browse files
authored
Revert "[IR] Don't store switch case values as operands" (#170962)
Reverts #166842 Breaks Mips LLVM tests, and LLD on bots. See #166842
1 parent 72d5fe5 commit 90e3ac6

File tree

13 files changed

+55
-141
lines changed

13 files changed

+55
-141
lines changed

llvm/docs/ReleaseNotes.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ Changes to the LLVM IR
7474
format string function implementations from statically-linked libc's based on
7575
the requirements of each call. Currently only `float` is supported; this can
7676
keep floating point support out of printf if it can be proven unused.
77-
* Case values are no longer operands of `SwitchInst`.
7877

7978
Changes to LLVM infrastructure
8079
------------------------------
@@ -179,7 +178,6 @@ Changes to the C API
179178
* Add `LLVMGetOrInsertFunction` to get or insert a function, replacing the combination of `LLVMGetNamedFunction` and `LLVMAddFunction`.
180179
* Allow `LLVMGetVolatile` to work with any kind of Instruction.
181180
* Add `LLVMConstFPFromBits` to get a constant floating-point value from an array of 64 bit values.
182-
* Add `LLVMGetSwitchCaseValue` and `LLVMSetSwitchCaseValue` to get and set switch case values; switch case values are no longer operands of the instruction.
183181

184182
Changes to the CodeGen infrastructure
185183
-------------------------------------

llvm/include/llvm-c/Core.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,30 +4213,6 @@ LLVM_C_ABI void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond);
42134213
*/
42144214
LLVM_C_ABI LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef SwitchInstr);
42154215

4216-
/**
4217-
* Obtain the case value for a successor of a switch instruction. i corresponds
4218-
* to the successor index. The first successor is the default destination, so i
4219-
* must be greater than zero.
4220-
*
4221-
* This only works on llvm::SwitchInst instructions.
4222-
*
4223-
* @see llvm::SwitchInst::CaseHandle::getCaseValue()
4224-
*/
4225-
LLVM_C_ABI LLVMValueRef LLVMGetSwitchCaseValue(LLVMValueRef SwitchInstr,
4226-
unsigned i);
4227-
4228-
/**
4229-
* Set the case value for a successor of a switch instruction. i corresponds to
4230-
* the successor index. The first successor is the default destination, so i
4231-
* must be greater than zero.
4232-
*
4233-
* This only works on llvm::SwitchInst instructions.
4234-
*
4235-
* @see llvm::SwitchInst::CaseHandle::setValue()
4236-
*/
4237-
LLVM_C_ABI void LLVMSetSwitchCaseValue(LLVMValueRef SwitchInstr, unsigned i,
4238-
LLVMValueRef CaseValue);
4239-
42404216
/**
42414217
* @}
42424218
*/

llvm/include/llvm/IR/Instructions.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,7 @@ class PHINode : public Instruction {
26652665
// User::allocHungoffUses, because we have to allocate Uses for the incoming
26662666
// values and pointers to the incoming blocks, all in one allocation.
26672667
void allocHungoffUses(unsigned N) {
2668-
User::allocHungoffUses(N, /*WithExtraValues=*/true);
2668+
User::allocHungoffUses(N, /* IsPhi */ true);
26692669
}
26702670

26712671
public:
@@ -3198,10 +3198,10 @@ class SwitchInst : public Instruction {
31983198

31993199
unsigned ReservedSpace;
32003200

3201-
// Operand[0] = Value to switch on
3202-
// Operand[1] = Default basic block destination
3203-
// Operand[n] = BasicBlock to go to on match
3204-
// Values are stored after the Uses similar to PHINode's basic blocks.
3201+
// Operand[0] = Value to switch on
3202+
// Operand[1] = Default basic block destination
3203+
// Operand[2n ] = Value to match
3204+
// Operand[2n+1] = BasicBlock to go to on match
32053205
SwitchInst(const SwitchInst &SI);
32063206

32073207
/// Create a new switch instruction, specifying a value to switch on and a
@@ -3223,17 +3223,6 @@ class SwitchInst : public Instruction {
32233223

32243224
LLVM_ABI SwitchInst *cloneImpl() const;
32253225

3226-
void allocHungoffUses(unsigned N) {
3227-
User::allocHungoffUses(N, /*WithExtraValues=*/true);
3228-
}
3229-
3230-
ConstantInt *const *case_values() const {
3231-
return reinterpret_cast<ConstantInt *const *>(op_begin() + ReservedSpace);
3232-
}
3233-
ConstantInt **case_values() {
3234-
return reinterpret_cast<ConstantInt **>(op_begin() + ReservedSpace);
3235-
}
3236-
32373226
public:
32383227
void operator delete(void *Ptr) { User::operator delete(Ptr); }
32393228

@@ -3268,7 +3257,7 @@ class SwitchInst : public Instruction {
32683257
ConstantIntT *getCaseValue() const {
32693258
assert((unsigned)Index < SI->getNumCases() &&
32703259
"Index out the number of cases.");
3271-
return SI->case_values()[Index];
3260+
return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
32723261
}
32733262

32743263
/// Resolves successor for current case.
@@ -3310,7 +3299,7 @@ class SwitchInst : public Instruction {
33103299
void setValue(ConstantInt *V) const {
33113300
assert((unsigned)Index < SI->getNumCases() &&
33123301
"Index out the number of cases.");
3313-
SI->case_values()[Index] = V;
3302+
SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
33143303
}
33153304

33163305
/// Sets the new successor for current case.
@@ -3417,7 +3406,9 @@ class SwitchInst : public Instruction {
34173406

34183407
/// Return the number of 'cases' in this switch instruction, excluding the
34193408
/// default case.
3420-
unsigned getNumCases() const { return getNumOperands() - 2; }
3409+
unsigned getNumCases() const {
3410+
return getNumOperands()/2 - 1;
3411+
}
34213412

34223413
/// Returns a read/write iterator that points to the first case in the
34233414
/// SwitchInst.
@@ -3519,14 +3510,14 @@ class SwitchInst : public Instruction {
35193510
/// case.
35203511
LLVM_ABI CaseIt removeCase(CaseIt I);
35213512

3522-
unsigned getNumSuccessors() const { return getNumOperands() - 1; }
3513+
unsigned getNumSuccessors() const { return getNumOperands()/2; }
35233514
BasicBlock *getSuccessor(unsigned idx) const {
35243515
assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3525-
return cast<BasicBlock>(getOperand(idx + 1));
3516+
return cast<BasicBlock>(getOperand(idx*2+1));
35263517
}
35273518
void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
35283519
assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3529-
setOperand(idx + 1, NewSucc);
3520+
setOperand(idx * 2 + 1, NewSucc);
35303521
}
35313522

35323523
// Methods for support type inquiry through isa, cast, and dyn_cast:

llvm/include/llvm/IR/User.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ class User : public Value {
132132

133133
/// Allocate the array of Uses, followed by a pointer
134134
/// (with bottom bit set) to the User.
135-
/// \param WithExtraValues identifies callers which need N Value* allocated
136-
/// along the N operands.
137-
LLVM_ABI void allocHungoffUses(unsigned N, bool WithExtraValues = false);
135+
/// \param IsPhi identifies callers which are phi nodes and which need
136+
/// N BasicBlock* allocated along with N
137+
LLVM_ABI void allocHungoffUses(unsigned N, bool IsPhi = false);
138138

139139
/// Grow the number of hung off uses. Note that allocHungoffUses
140140
/// should be called if there are no uses.
141-
LLVM_ABI void growHungoffUses(unsigned N, bool WithExtraValues = false);
141+
LLVM_ABI void growHungoffUses(unsigned N, bool IsPhi = false);
142142

143143
protected:
144144
~User() = default; // Use deleteValue() to delete a generic Instruction.

llvm/lib/Bitcode/Writer/ValueEnumerator.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ static OrderMap orderModule(const Module &M) {
164164
orderConstantValue(Op);
165165
if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
166166
orderValue(SVI->getShuffleMaskForBitcode(), OM);
167-
if (auto *SI = dyn_cast<SwitchInst>(&I)) {
168-
for (const auto &Case : SI->cases())
169-
orderValue(Case.getCaseValue(), OM);
170-
}
171167
orderValue(&I, OM);
172168
}
173169
}
@@ -1096,10 +1092,6 @@ void ValueEnumerator::incorporateFunction(const Function &F) {
10961092
}
10971093
if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
10981094
EnumerateValue(SVI->getShuffleMaskForBitcode());
1099-
if (auto *SI = dyn_cast<SwitchInst>(&I)) {
1100-
for (const auto &Case : SI->cases())
1101-
EnumerateValue(Case.getCaseValue());
1102-
}
11031095
}
11041096
BasicBlocks.push_back(&BB);
11051097
ValueMap[&BB] = BasicBlocks.size();

llvm/lib/CodeGen/TypePromotion.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,6 @@ void IRPromoter::PromoteTree() {
512512
I->setOperand(i, ConstantInt::get(ExtTy, 0));
513513
}
514514

515-
// For switch, also mutate case values, which are not operands.
516-
if (auto *SI = dyn_cast<SwitchInst>(I)) {
517-
for (auto Case : SI->cases()) {
518-
APInt NewConst = Case.getCaseValue()->getValue().zext(PromotedWidth);
519-
Case.setValue(ConstantInt::get(SI->getContext(), NewConst));
520-
}
521-
}
522-
523515
// Mutate the result type, unless this is an icmp or switch.
524516
if (!isa<ICmpInst>(I) && !isa<SwitchInst>(I)) {
525517
I->mutateType(ExtTy);

llvm/lib/IR/Core.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,19 +3257,6 @@ LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
32573257
return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
32583258
}
32593259

3260-
LLVMValueRef LLVMGetSwitchCaseValue(LLVMValueRef Switch, unsigned i) {
3261-
assert(i > 0 && i <= unwrap<SwitchInst>(Switch)->getNumCases());
3262-
auto It = unwrap<SwitchInst>(Switch)->case_begin() + (i - 1);
3263-
return wrap(It->getCaseValue());
3264-
}
3265-
3266-
void LLVMSetSwitchCaseValue(LLVMValueRef Switch, unsigned i,
3267-
LLVMValueRef CaseValue) {
3268-
assert(i > 0 && i <= unwrap<SwitchInst>(Switch)->getNumCases());
3269-
auto It = unwrap<SwitchInst>(Switch)->case_begin() + (i - 1);
3270-
It->setValue(unwrap<ConstantInt>(CaseValue));
3271-
}
3272-
32733260
/*--.. Operations on alloca instructions (only) ............................--*/
32743261

32753262
LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {

llvm/lib/IR/Instructions.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void PHINode::growOperands() {
202202
if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
203203

204204
ReservedSpace = NumOps;
205-
growHungoffUses(ReservedSpace, /*WithExtraValues=*/true);
205+
growHungoffUses(ReservedSpace, /* IsPhi */ true);
206206
}
207207

208208
/// hasConstantValue - If the specified PHI node always merges together the same
@@ -4076,20 +4076,18 @@ SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
40764076
InsertPosition InsertBefore)
40774077
: Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
40784078
AllocMarker, InsertBefore) {
4079-
init(Value, Default, 2 + NumCases);
4079+
init(Value, Default, 2+NumCases*2);
40804080
}
40814081

40824082
SwitchInst::SwitchInst(const SwitchInst &SI)
40834083
: Instruction(SI.getType(), Instruction::Switch, AllocMarker) {
40844084
init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
40854085
setNumHungOffUseOperands(SI.getNumOperands());
40864086
Use *OL = getOperandList();
4087-
ConstantInt **VL = case_values();
40884087
const Use *InOL = SI.getOperandList();
4089-
ConstantInt *const *InVL = SI.case_values();
4090-
for (unsigned i = 2, E = SI.getNumOperands(); i != E; ++i) {
4088+
for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
40914089
OL[i] = InOL[i];
4092-
VL[i - 2] = InVL[i - 2];
4090+
OL[i+1] = InOL[i+1];
40934091
}
40944092
SubclassOptionalData = SI.SubclassOptionalData;
40954093
}
@@ -4099,11 +4097,11 @@ SwitchInst::SwitchInst(const SwitchInst &SI)
40994097
void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
41004098
unsigned NewCaseIdx = getNumCases();
41014099
unsigned OpNo = getNumOperands();
4102-
if (OpNo + 1 > ReservedSpace)
4100+
if (OpNo+2 > ReservedSpace)
41034101
growOperands(); // Get more space!
41044102
// Initialize some new operands.
4105-
assert(OpNo < ReservedSpace && "Growing didn't work!");
4106-
setNumHungOffUseOperands(OpNo + 1);
4103+
assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
4104+
setNumHungOffUseOperands(OpNo+2);
41074105
CaseHandle Case(this, NewCaseIdx);
41084106
Case.setValue(OnVal);
41094107
Case.setSuccessor(Dest);
@@ -4114,22 +4112,21 @@ void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
41144112
SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
41154113
unsigned idx = I->getCaseIndex();
41164114

4117-
assert(2 + idx < getNumOperands() && "Case index out of range!!!");
4115+
assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
41184116

41194117
unsigned NumOps = getNumOperands();
41204118
Use *OL = getOperandList();
4121-
ConstantInt **VL = case_values();
41224119

41234120
// Overwrite this case with the end of the list.
4124-
if (2 + idx + 1 != NumOps) {
4125-
OL[2 + idx] = OL[NumOps - 1];
4126-
VL[idx] = VL[NumOps - 2 - 1];
4121+
if (2 + (idx + 1) * 2 != NumOps) {
4122+
OL[2 + idx * 2] = OL[NumOps - 2];
4123+
OL[2 + idx * 2 + 1] = OL[NumOps - 1];
41274124
}
41284125

41294126
// Nuke the last value.
4130-
OL[NumOps - 1].set(nullptr);
4131-
VL[NumOps - 2 - 1] = nullptr;
4132-
setNumHungOffUseOperands(NumOps - 1);
4127+
OL[NumOps-2].set(nullptr);
4128+
OL[NumOps-2+1].set(nullptr);
4129+
setNumHungOffUseOperands(NumOps-2);
41334130

41344131
return CaseIt(this, idx);
41354132
}
@@ -4142,7 +4139,7 @@ void SwitchInst::growOperands() {
41424139
unsigned NumOps = e*3;
41434140

41444141
ReservedSpace = NumOps;
4145-
growHungoffUses(ReservedSpace, /*WithExtraValues=*/true);
4142+
growHungoffUses(ReservedSpace);
41464143
}
41474144

41484145
void SwitchInstProfUpdateWrapper::init() {

llvm/lib/IR/User.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ bool User::replaceUsesOfWith(Value *From, Value *To) {
5050
// User allocHungoffUses Implementation
5151
//===----------------------------------------------------------------------===//
5252

53-
void User::allocHungoffUses(unsigned N, bool WithExtraValues) {
53+
void User::allocHungoffUses(unsigned N, bool IsPhi) {
5454
assert(HasHungOffUses && "alloc must have hung off uses");
5555

56-
static_assert(alignof(Use) >= alignof(Value *),
56+
static_assert(alignof(Use) >= alignof(BasicBlock *),
5757
"Alignment is insufficient for 'hung-off-uses' pieces");
5858

5959
// Allocate the array of Uses
6060
size_t size = N * sizeof(Use);
61-
if (WithExtraValues)
62-
size += N * sizeof(Value *);
61+
if (IsPhi)
62+
size += N * sizeof(BasicBlock *);
6363
Use *Begin = static_cast<Use*>(::operator new(size));
6464
Use *End = Begin + N;
6565
setOperandList(Begin);
6666
for (; Begin != End; Begin++)
6767
new (Begin) Use(this);
6868
}
6969

70-
void User::growHungoffUses(unsigned NewNumUses, bool WithExtraValues) {
70+
void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
7171
assert(HasHungOffUses && "realloc must have hung off uses");
7272

7373
unsigned OldNumUses = getNumOperands();
@@ -77,22 +77,22 @@ void User::growHungoffUses(unsigned NewNumUses, bool WithExtraValues) {
7777
assert(NewNumUses > OldNumUses && "realloc must grow num uses");
7878

7979
Use *OldOps = getOperandList();
80-
allocHungoffUses(NewNumUses, WithExtraValues);
80+
allocHungoffUses(NewNumUses, IsPhi);
8181
Use *NewOps = getOperandList();
8282

8383
// Now copy from the old operands list to the new one.
8484
std::copy(OldOps, OldOps + OldNumUses, NewOps);
8585

86-
// If the User has extra values (phi basic blocks, switch case values), then
87-
// we need to copy these, too.
88-
if (WithExtraValues) {
86+
// If this is a Phi, then we need to copy the BB pointers too.
87+
if (IsPhi) {
8988
auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
9089
auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
91-
std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(Value *)), NewPtr);
90+
std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
9291
}
9392
Use::zap(OldOps, OldOps + OldNumUses, true);
9493
}
9594

95+
9696
// This is a private struct used by `User` to track the co-allocated descriptor
9797
// section.
9898
struct DescriptorInfo {

llvm/lib/IR/Verifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3424,7 +3424,7 @@ void Verifier::visitSwitchInst(SwitchInst &SI) {
34243424
Type *SwitchTy = SI.getCondition()->getType();
34253425
SmallPtrSet<ConstantInt*, 32> Constants;
34263426
for (auto &Case : SI.cases()) {
3427-
Check(isa<ConstantInt>(Case.getCaseValue()),
3427+
Check(isa<ConstantInt>(SI.getOperand(Case.getCaseIndex() * 2 + 2)),
34283428
"Case value is not a constant integer.", &SI);
34293429
Check(Case.getCaseValue()->getType() == SwitchTy,
34303430
"Switch constants must all be same type as switch value!", &SI);

0 commit comments

Comments
 (0)