Skip to content

Commit 9de597d

Browse files
author
Dave Bartolomeo
committed
C++: Refactor Operand to prepare for cross-phase IPA sharing
1 parent 23532ae commit 9de597d

File tree

7 files changed

+255
-214
lines changed

7 files changed

+255
-214
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,45 @@ private newtype TOperand =
2727
defInstr = Construction::getPhiOperandDefinition(useInstr, predecessorBlock, overlap)
2828
}
2929

30+
private class OperandBase extends TOperand {
31+
abstract string toString();
32+
}
33+
34+
private class RegisterOperandBase extends OperandBase, TRegisterOperand {
35+
abstract override string toString();
36+
}
37+
38+
private RegisterOperandBase registerOperand(
39+
Instruction useInstr, RegisterOperandTag tag, Instruction defInstr
40+
) {
41+
result = TRegisterOperand(useInstr, tag, defInstr)
42+
}
43+
44+
private class NonPhiMemoryOperandBase extends OperandBase, TNonPhiMemoryOperand {
45+
abstract override string toString();
46+
}
47+
48+
private NonPhiMemoryOperandBase nonPhiMemoryOperand(
49+
Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap
50+
) {
51+
result = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap)
52+
}
53+
54+
private class PhiOperandBase extends OperandBase, TPhiOperand {
55+
abstract override string toString();
56+
}
57+
58+
private PhiOperandBase phiOperand(
59+
Instruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap
60+
) {
61+
result = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap)
62+
}
63+
3064
/**
3165
* A source operand of an `Instruction`. The operand represents a value consumed by the instruction.
3266
*/
33-
class Operand extends TOperand {
34-
string toString() { result = "Operand" }
67+
class Operand extends OperandBase {
68+
override string toString() { result = "Operand" }
3569

3670
final Language::Location getLocation() { result = getUse().getLocation() }
3771

@@ -165,8 +199,8 @@ class Operand extends TOperand {
165199
*/
166200
class MemoryOperand extends Operand {
167201
MemoryOperand() {
168-
this = TNonPhiMemoryOperand(_, _, _, _) or
169-
this = TPhiOperand(_, _, _, _)
202+
this instanceof NonPhiMemoryOperandBase or
203+
this instanceof PhiOperandBase
170204
}
171205

172206
/**
@@ -204,8 +238,8 @@ class NonPhiOperand extends Operand {
204238
OperandTag tag;
205239

206240
NonPhiOperand() {
207-
this = TRegisterOperand(useInstr, tag, defInstr) or
208-
this = TNonPhiMemoryOperand(useInstr, tag, defInstr, _)
241+
this = registerOperand(useInstr, tag, defInstr) or
242+
this = nonPhiMemoryOperand(useInstr, tag, defInstr, _)
209243
}
210244

211245
final override Instruction getUse() { result = useInstr }
@@ -222,21 +256,25 @@ class NonPhiOperand extends Operand {
222256
/**
223257
* An operand that consumes a register (non-memory) result.
224258
*/
225-
class RegisterOperand extends NonPhiOperand, TRegisterOperand {
259+
class RegisterOperand extends NonPhiOperand, RegisterOperandBase {
226260
override RegisterOperandTag tag;
227261

262+
final override string toString() { result = tag.toString() }
263+
228264
final override Overlap getDefinitionOverlap() {
229265
// All register results overlap exactly with their uses.
230266
result instanceof MustExactlyOverlap
231267
}
232268
}
233269

234-
class NonPhiMemoryOperand extends NonPhiOperand, MemoryOperand, TNonPhiMemoryOperand {
270+
class NonPhiMemoryOperand extends NonPhiOperand, MemoryOperand, NonPhiMemoryOperandBase {
235271
override MemoryOperandTag tag;
236272
Overlap overlap;
237273

238274
NonPhiMemoryOperand() { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) }
239275

276+
final override string toString() { result = tag.toString() }
277+
240278
final override Overlap getDefinitionOverlap() { result = overlap }
241279
}
242280

@@ -254,8 +292,6 @@ class TypedOperand extends NonPhiMemoryOperand {
254292
*/
255293
class AddressOperand extends RegisterOperand {
256294
override AddressOperandTag tag;
257-
258-
override string toString() { result = "Address" }
259295
}
260296

261297
/**
@@ -264,8 +300,6 @@ class AddressOperand extends RegisterOperand {
264300
*/
265301
class BufferSizeOperand extends RegisterOperand {
266302
override BufferSizeOperandTag tag;
267-
268-
override string toString() { result = "BufferSize" }
269303
}
270304

271305
/**
@@ -274,62 +308,48 @@ class BufferSizeOperand extends RegisterOperand {
274308
*/
275309
class LoadOperand extends TypedOperand {
276310
override LoadOperandTag tag;
277-
278-
override string toString() { result = "Load" }
279311
}
280312

281313
/**
282314
* The source value operand of a `Store` instruction.
283315
*/
284316
class StoreValueOperand extends RegisterOperand {
285317
override StoreValueOperandTag tag;
286-
287-
override string toString() { result = "StoreValue" }
288318
}
289319

290320
/**
291321
* The sole operand of a unary instruction (e.g. `Convert`, `Negate`, `Copy`).
292322
*/
293323
class UnaryOperand extends RegisterOperand {
294324
override UnaryOperandTag tag;
295-
296-
override string toString() { result = "Unary" }
297325
}
298326

299327
/**
300328
* The left operand of a binary instruction (e.g. `Add`, `CompareEQ`).
301329
*/
302330
class LeftOperand extends RegisterOperand {
303331
override LeftOperandTag tag;
304-
305-
override string toString() { result = "Left" }
306332
}
307333

308334
/**
309335
* The right operand of a binary instruction (e.g. `Add`, `CompareEQ`).
310336
*/
311337
class RightOperand extends RegisterOperand {
312338
override RightOperandTag tag;
313-
314-
override string toString() { result = "Right" }
315339
}
316340

317341
/**
318342
* The condition operand of a `ConditionalBranch` or `Switch` instruction.
319343
*/
320344
class ConditionOperand extends RegisterOperand {
321345
override ConditionOperandTag tag;
322-
323-
override string toString() { result = "Condition" }
324346
}
325347

326348
/**
327349
* The operand representing the target function of an `Call` instruction.
328350
*/
329351
class CallTargetOperand extends RegisterOperand {
330352
override CallTargetOperandTag tag;
331-
332-
override string toString() { result = "CallTarget" }
333353
}
334354

335355
/**
@@ -347,43 +367,34 @@ class ArgumentOperand extends RegisterOperand {
347367
*/
348368
class ThisArgumentOperand extends ArgumentOperand {
349369
override ThisArgumentOperandTag tag;
350-
351-
override string toString() { result = "ThisArgument" }
352370
}
353371

354372
/**
355373
* An operand representing an argument to a function call.
356374
*/
357375
class PositionalArgumentOperand extends ArgumentOperand {
358376
override PositionalArgumentOperandTag tag;
359-
int argIndex;
360-
361-
PositionalArgumentOperand() { argIndex = tag.getArgIndex() }
362-
363-
override string toString() { result = "Arg(" + argIndex + ")" }
364377

365378
/**
366379
* Gets the zero-based index of the argument.
367380
*/
368-
final int getIndex() { result = argIndex }
381+
final int getIndex() { result = tag.getArgIndex() }
369382
}
370383

371384
class SideEffectOperand extends TypedOperand {
372385
override SideEffectOperandTag tag;
373-
374-
override string toString() { result = "SideEffect" }
375386
}
376387

377388
/**
378389
* An operand of a `PhiInstruction`.
379390
*/
380-
class PhiInputOperand extends MemoryOperand, TPhiOperand {
391+
class PhiInputOperand extends MemoryOperand, PhiOperandBase {
381392
PhiInstruction useInstr;
382393
Instruction defInstr;
383394
IRBlock predecessorBlock;
384395
Overlap overlap;
385396

386-
PhiInputOperand() { this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) }
397+
PhiInputOperand() { this = phiOperand(useInstr, defInstr, predecessorBlock, overlap) }
387398

388399
override string toString() { result = "Phi" }
389400

@@ -413,8 +424,6 @@ class PhiInputOperand extends MemoryOperand, TPhiOperand {
413424
class ChiTotalOperand extends NonPhiMemoryOperand {
414425
override ChiTotalOperandTag tag;
415426

416-
override string toString() { result = "ChiTotal" }
417-
418427
final override MemoryAccessKind getMemoryAccess() { result instanceof ChiTotalMemoryAccess }
419428
}
420429

@@ -424,7 +433,5 @@ class ChiTotalOperand extends NonPhiMemoryOperand {
424433
class ChiPartialOperand extends NonPhiMemoryOperand {
425434
override ChiPartialOperandTag tag;
426435

427-
override string toString() { result = "ChiPartial" }
428-
429436
final override MemoryAccessKind getMemoryAccess() { result instanceof ChiPartialMemoryAccess }
430437
}

cpp/ql/src/semmle/code/cpp/ir/implementation/internal/OperandTag.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ PositionalArgumentOperandTag positionalArgumentOperand(int argIndex) {
221221
result = TPositionalArgumentOperand(argIndex)
222222
}
223223

224-
class ChiTotalOperandTag extends MemoryOperandTag, TChiTotalOperand {
224+
abstract class ChiOperandTag extends MemoryOperandTag {
225+
}
226+
227+
class ChiTotalOperandTag extends ChiOperandTag, TChiTotalOperand {
225228
final override string toString() { result = "ChiTotal" }
226229

227230
final override int getSortOrder() { result = 13 }
@@ -231,7 +234,7 @@ class ChiTotalOperandTag extends MemoryOperandTag, TChiTotalOperand {
231234

232235
ChiTotalOperandTag chiTotalOperand() { result = TChiTotalOperand() }
233236

234-
class ChiPartialOperandTag extends MemoryOperandTag, TChiPartialOperand {
237+
class ChiPartialOperandTag extends ChiOperandTag, TChiPartialOperand {
235238
final override string toString() { result = "ChiPartial" }
236239

237240
final override int getSortOrder() { result = 14 }

0 commit comments

Comments
 (0)