Shares of Taiwan Semiconductor Manufacturing (TSM -0.34%), popularly known as TSMC, jumped over 2% following the release of the company's second-quarter results on July 14, 2022. The foundry giant handily beat Wall Street's expectations thanks to the growing demand for chips deployed in the Internet of Things (IoT), automotive, and high-performance computing (HPC) markets.

What's more, TSMC's guidance was the icing on the cake and should assuage the fears of a slowdown in semiconductor demand that has been weighing on investors' minds and pulling the sector down. Let's look at how TSMC performed last quarter and check whether buying the stock right now looks like a good idea.

TSMC is on a roll

TSMC's second-quarter revenue shot up 36.6% year over year to $18.16 billion, which was higher than the consensus estimate of $17.68 billion. The company's gross margin increased to 59.1% last quarter from 50% in the prior-year period. The operating margin also increased by an impressive 10 percentage points year over year.

The solid top-line growth and the fatter margins led to a 67% year-over-year increase in TSMC's earnings to $1.55 per share. Analysts were looking for earnings of $1.44 per share. The Taiwanese giant's robust growth was driven by the healthy demand for advanced chips based on 7-nanometer (nm) and 5-nanometer manufacturing nodes.

TSMC's diversified end markets allowed it to navigate the weakness in the smartphone space successfully. Smartphone shipments are shrinking this year due to weakness in demand and supply chain disruptions, and that could have thrown TSMC off gear, as it gets 38% of its revenue from selling smartphone chips. TSMC witnessed just 3% quarter-over-quarter growth in revenue from smartphone chips last quarter.

But the HPC, IoT, and automotive markets that together produced 56% of its revenue last quarter helped it beat the tepid growth in smartphone sales. HPC was TSMC's biggest segment in Q2 with 43% of the total revenue, and the segment's sales were up 13% over the prior quarter. IoT and automotive revenue increased 14% quarter over quarter.

TSMC also issued a solid forecast. The company expects $20.2 billion in revenue this quarter at the midpoint of its guidance range, which would translate into a 36% year-over-year increase. The operating margin is expected at 48% at the midpoint, a nice jump over the year-ago quarter's 41.2%. Even better, TSMC management pointed out on the latest earnings conference call that it expects full-year revenue growth of mid-30%.

That's impressive considering that TSMC indicated soft demand for chips due to inventory adjustments in the smartphone, personal computer (PC), and consumer markets. The company, however, points out that data center and automotive demand are steady, and it will be allocating capacity toward these markets.

In all, TSMC's diversification puts it in a nice position to weather the downturn that it is witnessing in certain areas.

The stock is an attractive bet

TSMC points out that the semiconductor market is built for long-term growth, even though there may be softness in the near term. The semiconductor content in various applications from data centers to smartphones to automotive is increasing, thereby creating a bigger revenue opportunity for TSMC to tap into. According to TSMC CEO C.C. Wei:

While the device unit growth of many electronics device[s] may be flattish to low single-digit percentage range, in the next several years, the silicon content growth will be higher, in the mid- to high single-digit percentage range and support the long-term structural semiconductor demand and increase our addressable wafer demand.

More importantly, TSMC leads the chip foundry market with an impressive share, which explains why it is confident about clocking a compound annual revenue growth rate of 15% to 20% "over the next several years." Analysts are also upbeat about the company's prospects, forecasting 20% annual earnings growth over the next five years.

All this makes TSMC a top semiconductor stock to buy right now, as it is trading for less than 20 times trailing earnings, a discount to the Nasdaq 100's multiple of nearly 25.